const mongoose = require('mongoose');
const User = require('../models/User');
require('dotenv').config();

async function createAdmin() {
  try {
    await mongoose.connect(process.env.MONGODB_URI);
    
    const adminData = {
      username: 'admin',
      email: 'admin@example.com',
      password: 'admin123456',
      role: 'admin'
    };

    const existingAdmin = await User.findOne({ email: adminData.email });
    if (existingAdmin) {
      console.log('管理员账号已存在');
      return;
    }

    const admin = new User(adminData);
    await admin.save();
    
    console.log('管理员账号创建成功');
    console.log('邮箱:', adminData.email);
    console.log('密码:', adminData.password);
  } catch (error) {
    console.error('创建管理员账号失败:', error);
  } finally {
    mongoose.connection.close();
  }
}

createAdmin(); 